#include "ConfirmClientInterface.h"
#include "ui_confirmclient.h"
#include "socket/csocketcommunication.h"
#include "globalParameterForTaxi.h"
#include "QMessageBox"
#include "socket/protocol/CarAbandonBook.h"
#include "QFont"

extern ORDER_INFO g_orderInfo;
extern DRIVER_NAME g_driverInfo;
extern CHOSEN_CLIENT_INFO g_chosenClient;
extern const int CHARACTERLENGTH;

interface::CConfirmClientInterface::CConfirmClientInterface(IInterface *parent, \
                                                            CHOSEN_CLIENT_INFO *pChosenClientInfo)
    : IInterface(parent),
      m_pUi(new Ui::ConfirmClient)
{
    m_pUi->setupUi(this);

    // setup the background
    this->UiSetting(m_pUi);

    m_pCarChooseClient = NULL;
    m_pCarAbandonBook = NULL;

    // set the label text
    QPalette pa;
    pa.setColor(QPalette::WindowText,Qt::white);
    QFont qf;
    qf.setBold(true);
    qf.setPixelSize(20);

    m_pUi->labelPhone->setPalette(pa);
    m_pUi->labelPhone->setFont(qf);
    m_pUi->labelName->setPalette(pa);
    m_pUi->labelName->setFont(qf);
    m_pUi->labelArriveTime->setPalette(pa);
    m_pUi->labelArriveTime->setFont(qf);

    // we have to admit that *pChosenClientInfo == g_chosenCar, so do not need to delete pChosenClientInfo
    m_pUi->labelPhone->setText(pChosenClientInfo->mobile_phone);
    m_pUi->labelName->setText(pChosenClientInfo->client_name);
    m_pUi->labelArriveTime->setText(QString::number(pChosenClientInfo->arrivingTime).append(" mins") );

    // connect between buttons and actions
    connect(m_pUi->btnCancel, SIGNAL(clicked()), this, SLOT(cancel()) );
    connect(m_pUi->btnOK, SIGNAL(clicked()), this, SLOT(confirm()) );

    //connect the signals from socket
    connect(CSocketCommunication::GetInstance(), SIGNAL(canRead()), this, SLOT(readResult()) );
    connect(CSocketCommunication::GetInstance(), SIGNAL(socketError()), this, SLOT(socketError()) );
}

interface::CConfirmClientInterface::~CConfirmClientInterface()
{
    delete m_pUi;

    if(m_pCarChooseClient != NULL)
    {
        delete m_pCarChooseClient;
        m_pCarChooseClient = NULL;
    }

    if(m_pCarAbandonBook != NULL)
    {
        delete m_pCarAbandonBook;
        m_pCarAbandonBook = NULL;
    }
}

void interface::CConfirmClientInterface::UiSetting(Ui::ConfirmClient *ui)
{
    //setup the background
    IInterface::SetupBackground(QString("images\\ConfirmCar\\background.jpg"));

    //setup the login button
    IInterface::SetupBtn(ui->btnOK, QString("images\\ConfirmCar\\ok.png"));

    //setup the exit button
    IInterface::SetupBtn(ui->btnCancel, QString("images\\ConfirmCar\\cancel.png"));
}

// the user has confirmed a taxi, and should send the car id and user id to the server
void interface::CConfirmClientInterface::confirm()
{
    m_pCarChooseClient = new protocal::CCarChooseClient();

    QString user_id = g_chosenClient.client_id;

    // we need to complete the string to length 10
    while(user_id.length() < CHARACTERLENGTH - 1)
    {
        user_id.insert(user_id.length(), QChar('\t'));
    }
    memcpy(m_pCarChooseClient->userID, user_id.toStdString().c_str(), user_id.toStdString().length() + 1);

    QString car_id = g_driverInfo.car_id;
    // we need to complete the string to length 10
    while(car_id.length() < CHARACTERLENGTH - 1)
    {
        car_id.insert(car_id.length(), QChar('\t'));
    }
    memcpy(m_pCarChooseClient->carID, car_id.toStdString().c_str(), car_id.toStdString().length() + 1);

    // send out the request
    CSocketCommunication::GetInstance()->sendRequest(protocal::CAR_CHOOSE_CLIENT, (void*)m_pCarChooseClient, sizeof(protocal::CCarChooseClient));
}

// the car has cancelled the order, and should send the failure info to the client
void interface::CConfirmClientInterface::cancel()
{
    // TODO: confirm whether the server has dealt with CLI_ABANDON_BOOK
    // send out the request
    m_pCarAbandonBook = new protocal::CarAbandonBook;

    QString user_id = g_chosenClient.client_id;

    // we need to complete the string to length 10
    while(user_id.length() < CHARACTERLENGTH - 1)
    {
        user_id.insert(user_id.length(), QChar('\t'));
    }
    memcpy(m_pCarAbandonBook->driver_id, user_id.toStdString().c_str(), user_id.toStdString().length() + 1);

    // tell the server to notify all the related drivers that he is not the target.
    CSocketCommunication::GetInstance()->sendRequest(protocal::CAR_ABANDON_BOOK, \
                                                     (void*)m_pCarAbandonBook, \
                                                     sizeof(protocal::CarAbandonBook));

    // no need to wait for the server, and jump back to the server choose
    emit changeInterfaces(interface::CONFIRM_CLIENT, interface::SERVICE_CHOOSE, NULL);
}

// slots for socket communication
void interface::CConfirmClientInterface::readResult()
{
    // here we have received the responds from the server
    if (CSocketCommunication::GetInstance()->getResult(QString("isSuccess")).toInt() == 0)
    {
        // order fails, for the taxi the user wants is booked by others
        QMessageBox::information(this, tr("warning in confirming car"), \
                                 QString("That car has been booked by others").append(tr("\nplease try again!")));
        return;
    }

    // get new info from server
    g_chosenClient.mobile_phone = CSocketCommunication::GetInstance()->getResult(QString("mobileNumber"));
    g_orderInfo.order_id = CSocketCommunication::GetInstance()->getResult(QString("orderId"));
    g_orderInfo.pClientInfo = &g_chosenClient;

    // send the result to the next interface
    emit changeInterfaces(interface::CONFIRM_CLIENT, interface::DRIVING_TO_CLIENT, (void*)&g_orderInfo);
}

void interface::CConfirmClientInterface::socketError()
{
    QMessageBox::information(this, tr("warning in confirming car"), \
                             CSocketCommunication::GetInstance()->getErrorString().append(tr("\nplease try again!")));
}
